﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PPC_emulator
{
    class OPDecipherer
    {
        public bool isValid(string input)
        {
            if(!input.Contains(" "))
                return false;
            return true;
        }

        public OPCodes getOPC(string input)
        {
            String[] splitters = { " ", "," };
            String[] OP = input.Split(splitters, StringSplitOptions.RemoveEmptyEntries);
            foreach (string opc in Enum.GetNames(typeof(OPCodes)))
            {
                if(opc.Equals(OP[0].ToLower()))
                    return (OPCodes)Enum.Parse(typeof(OPCodes), opc);
            }
            return OPCodes.ERROR;
        }

        public int getrD(string input)
        {
            String[] splitters = { " ", "," };
            String[] OP = input.Split(splitters, StringSplitOptions.RemoveEmptyEntries);
            int register;
            if (OP[1].Substring(0, 1).ToLower().Equals("r"))
                register = (int.Parse(OP[1].Substring(1)));
            else   
                register = int.Parse(OP[1]);
            if (register < 0 || register > 31)
                register = -1;
            return register;
        }

        public int getrA(string input)
        {
            String[] splitters = { " ", "," };
            String[] OP = input.Split(splitters, StringSplitOptions.RemoveEmptyEntries);
            int register;
            if (OP[2].Substring(0, 1).ToLower().Equals("r"))
                register = (int.Parse(OP[2].Substring(1)));
            else
                register = int.Parse(OP[2]);
            if (register < 0 || register > 31)
                register = -1;
            return register;
        }

        public int getrB(string input)
        {
            String[] splitters = { " ", "," };
            String[] OP = input.Split(splitters, StringSplitOptions.RemoveEmptyEntries);
            int register;
            if (OP[3].Substring(0, 1).ToLower().Equals("r"))
                register = (int.Parse(OP[3].Substring(1)));
            else
                register = int.Parse(OP[3]);
            if (register < 0 || register > 31)
                register = -1;
            return register;
        }

        public UInt16 getValue(string input)
        {
            String[] splitters = { " ", "," };
            String[] OP = input.Split(splitters, StringSplitOptions.RemoveEmptyEntries);
            if(OP[2].Length < 2)
                return UInt16.Parse(OP[2]);
            if(OP[2].Substring(0,2).Equals("0x"))
                return UInt16.Parse(OP[2].Substring(2), System.Globalization.NumberStyles.AllowHexSpecifier);
            return UInt16.Parse(OP[2]);
        }

        public UInt16 getValue2(string input)
        {
            String[] splitters = { " ", "," };
            String[] OP = input.Split(splitters, StringSplitOptions.RemoveEmptyEntries);
            if (OP[3].Length < 2)
                return UInt16.Parse(OP[3]);
            if (OP[3].Substring(0, 2).Equals("0x"))
                return UInt16.Parse(OP[3].Substring(2), System.Globalization.NumberStyles.AllowHexSpecifier);
            return UInt16.Parse(OP[3]);
        }

        public UInt16 getd(string input)
        {
            String[] splitters = { " ", ",", "(", ")" };
            String[] OP = input.Split(splitters, StringSplitOptions.RemoveEmptyEntries);
            if (OP[2].Length < 2)
                return UInt16.Parse(OP[2]);
            if (OP[2].Substring(0, 2).Equals("0x"))
                return UInt16.Parse(OP[2].Substring(2), System.Globalization.NumberStyles.AllowHexSpecifier);
            return UInt16.Parse(OP[2]);
        }

        public int getrA2(string input)
        {
            String[] splitters = { " ", ",", "(", ")" };
            String[] OP = input.Split(splitters, StringSplitOptions.RemoveEmptyEntries);
            int register;
            if (OP[3].Substring(0, 1).ToLower().Equals("r"))
                register = (int.Parse(OP[3].Substring(1)));
            else
                register = int.Parse(OP[3]);
            if (register < 0 || register > 31)
                register = -1;
            return register;
        }
    }
}
